package org.water.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.water.base.entity.ResultBaseBean;
import org.water.base.web.BaseController;
import org.water.common.bind.annotation.MethodDesc;
import org.water.sys.entity.SysDictType;
import org.water.sys.entity.SysDictValue;
import org.water.sys.service.DictService;
/**
 * 字典控制器
 * @author qzy
 *
 */
@Controller
public class DictController extends BaseController {

	@Autowired
	private DictService dictService;
	/**
	 * 字典主页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/admin/dict/index")
	public String index(HttpServletRequest request,HttpServletResponse response)
	{
		return "admin/dict/dictIndex";
	}
	/**
	 * 查询字典类型
	 * @param name
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@MethodDesc(name="字典管理",value="查询字典类型")
	@RequestMapping("/welcome/dict/type/qry")
	public Page<SysDictType> findDictType(@RequestParam(name="name",defaultValue="",required=false) String name,@RequestParam("pageIndex") int pageIndex,@RequestParam("pageSize") int pageSize)
	{
		return dictService.findDictType(name, pageIndex, pageSize);
	}
	/**
	 * 根据字典类型编码查询
	 * @param code
	 * @return
	 */
	@RequestMapping("/welcome/dict/type/code/one")
	public SysDictType findDictTypeCodeOne(@RequestParam("typeCode")  String typeCode)
	{
		return dictService.findDictTypeOne(typeCode);
	}
	/**
	 * 
	 * @param typeCode
	 * @return
	 */
	@RequestMapping("/welcome/dict/type/code/own")
	public ResultBaseBean haseOwnDictTypeByCode(@RequestParam("typeCode")  String typeCode)
	{
		ResultBaseBean bean=getBaseBean(true, "");
		SysDictType dt=dictService.findDictTypeOne(typeCode);
		if(dt!=null)
		{
			bean.setSuccess(false);
		}	
		else
		{
			bean.setSuccess(true);
		}
		return bean;
	}
	
	/**
	 * 根据字典类型id查询
	 * @param code
	 * @return
	 */
	@RequestMapping("/welcome/dict/type/id/one")
	public SysDictType findDictTypeIdOne(@RequestParam("typeId")  String typeId)
	{
		return dictService.findOne(typeId);
	}
	/**
	 * 根据字典类型查询字典值
	 * @param typeId 字典类型ID
	 * @return
	 */
	@RequestMapping("/welcome/dict/type/id/values")
	public List<SysDictValue> findByDictTypeId(@RequestParam("typeId")  String typeId)
	{
		return dictService.findByDictTypeId(typeId);
	}
	/**
	 * 根据字典类型查询字典值
	 * @param typeCode 字典类型编码
	 * @return
	 */
	@RequestMapping("/welcome/dict/type/code/values")
	public List<SysDictValue> findByDictTypeCode(@RequestParam("typeCode") String typeCode)
	{
		return dictService.findByDictTypeCode(typeCode);
	}
	/**
	 * 根据字典值id查询
	 * @param code
	 * @return
	 */
	@RequestMapping("/welcome/dict/value/id/one")
	public SysDictValue findDictValueIdOne(@RequestParam("valueId")  String valueId)
	{
		return dictService.findDictValue(valueId);
	}
	/**
	 * 根据字典类型编码和值编码查询
	 * @param typeCode
	 * @param valueCode
	 * @return
	 */
	@RequestMapping("/welcome/dict/type/code/value")
	public SysDictValue findByTypeCodeAndValueCode(String typeCode,String valueCode)
	{
	   return dictService.findByTypeCodeAndValueCode(typeCode, valueCode);	
	}
	/**
	 * 查询字典值是否存在
	 * @param typeCode 
	 * @param valueCode
	 * @return
	 */
	@RequestMapping("/welcome/dict/value/code/own")
	public ResultBaseBean haseOwnDictValueByCode(@RequestParam("typeCode")  String typeCode,@RequestParam("valueCode") String valueCode)
	{
		ResultBaseBean bean=getBaseBean(true, "");
		SysDictValue dv=dictService.findByTypeCodeAndValueCode(typeCode,valueCode);
		if(dv!=null)
		{
			bean.setSuccess(false);
		}	
		else
		{
			bean.setSuccess(true);
		}
		return bean;
	}
	/**
	 * 保存字典类型
	 * @param dictType
	 * @return
	 */
	@CacheEvict(value="sysDictCache",allEntries=true)
	@MethodDesc(name="字典管理",value="保存字典类型")
	@RequestMapping("/admin/dict/type/save")
	public ResultBaseBean saveDictType(SysDictType dictType)
	{
		dictService.save(dictType);
		return getBaseBean(true, "");
	}
	/**
	 * 保存字典类型值
	 * @param dictType
	 * @return
	 */
	@CacheEvict(value="sysDictCache",allEntries=true)
	@MethodDesc(name="字典管理",value="保存字典值")
	@RequestMapping("/admin/dict/value/save")
	public ResultBaseBean saveDictValue(SysDictValue dictValue)
	{
		dictService.save(dictValue);
		return getBaseBean(true, "");
	}
	
	/**
	 * 删除字典类型
	 * @param dictType
	 * @return
	 */
	@CacheEvict(value="sysDictCache",allEntries=true)
	@MethodDesc(name="字典管理",value="删除字典类型")
	@RequestMapping("/admin/dict/type/del")
	public ResultBaseBean delDictType(@RequestParam("typeId") String typeId)
	{
		dictService.del(typeId);
		return getBaseBean(true, "");
	}
	/**
	 * 删除字典类型值
	 * @param dictType
	 * @return
	 */
	@CacheEvict(value="sysDictCache",allEntries=true)
	@MethodDesc(name="字典管理",value="删除字典值")
	@RequestMapping("/admin/dict/value/del")
	public ResultBaseBean delDictValue(@RequestParam("valueId") String valueId)
	{
		dictService.delDictValue(valueId);
		return getBaseBean(true, "");
	}
}
